package m202403.d14;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @@author: 爱做梦的锤子
 * @date: 2024/3/14 15:09
 */
//139. 单词拆分
public class Q139 {

    public static void main(String[] args) {
        new Q139().wordBreak("leetcodeabc", List.of("leetcode", "leet", "codeabc"));
    }

    public boolean wordBreak(String s, List<String> wordDict) {
        boolean[] dp = new boolean[s.length() + 1];
        Set<String> set = new HashSet<>(wordDict);
        dp[0] = true;
        for (int i = 1; i <= s.length(); i++) {
            for (int j = 0; j < i; j++) {
                if(dp[j]&&set.contains(s.substring(j,i))){
                    dp[i] = true;
                    break;
                }
            }
        }
        return dp[s.length()];
    }
}
